/**
 * 
 */
package com.kebo2.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;

/**
 * @author Tim
 *This class is used to dynamically change the UI and contains the core UI elements 
 */
public class UserInterface {
	
	public static DockLayoutPanel systembar;
	public static HorizontalPanel dock;
	public static Button userButton;
	public static DockLayoutPanel mainPanel;
	public static FlowPanel menu;
	public static FlexTable iconFlexTable;
	public static Image closeImg;
	public static HTML closeTxt;
	public static Image exportImg;
	public static HTML exportTxt;
	public static DockLayoutPanel window;
	
	public static class notification {
		
		private HTML msg;
		private Image infoIcon;
		private Grid notification;
		
		public notification(String message) {
			notification = new Grid(1,2);
			
			infoIcon = new Image("img/info_16.png");
			notification.setWidget(0, 0, infoIcon);
			
			msg = new HTML("<b>" + message + "</b>");
			notification.setWidget(0, 1, msg);
			
			int widgetCount = systembar.getWidgetCount();
			if(widgetCount > 1) {
				systembar.remove(1);
			}
			systembar.addWest(notification, 100);
			
			Timer t = new Timer() {
				public void run() {
					notification.clear();
					notification.removeFromParent();
				}	
			};
			t.schedule(5000);	
		}
	}
	
	public static class Icon {
		
		public static Image saveImg;
		public static HTML saveTxt;
		public static Image deleteImg;
		public static HTML deleteTxt;
		public static Image editImg;
		public static HTML editTxt;
		public static Image searchImg;
		public static HTML searchTxt;
		public static Image nextImg;
		public static HTML nextTxt;
		
		public Icon(String type, int index) {
			
			if(type == "save") {
				saveImg = new Image("img/save.png");
				saveTxt = new HTML("Opslaan");
			
				iconFlexTable.setWidget(0, index, saveImg);
				iconFlexTable.setWidget(0, index + 1, saveTxt);
			
				saveTxt.addStyleName("iconTxt");
				saveImg.addStyleName("iconImg");
			
				saveTxt.setTitle("Opslaan");
				saveImg.setTitle("Opslaan");
			}
			if(type == "delete") {
				deleteImg = new Image("img/delete.png");
				deleteTxt = new HTML("Verwijder");
			
				iconFlexTable.setWidget(0, index, deleteImg);
				iconFlexTable.setWidget(0, index + 1, deleteTxt);
				
				deleteTxt.addStyleName("iconTxt");
				deleteImg.addStyleName("iconImg");
				
				deleteTxt.setTitle("Verwijder");
				deleteImg.setTitle("Verwijder");
			}
			if(type == "edit") {
				editImg = new Image("img/edit.png");
				editTxt = new HTML("Bewerk");
				
				iconFlexTable.setWidget(0, index, editImg);
				iconFlexTable.setWidget(0, index + 1, editTxt);
				
				editTxt.addStyleName("iconTxt");
				editImg.addStyleName("iconImg");
				
				editTxt.setTitle("Bewerk");
				editImg.setTitle("Bewerk");			
			}
			if(type == "search") {
				searchImg = new Image("img/search.png");
				searchTxt = new HTML("Zoek");
				
				iconFlexTable.setWidget(0, index, searchImg);
				iconFlexTable.setWidget(0, index + 1, searchTxt);
				
				searchTxt.addStyleName("iconTxt");
				searchImg.addStyleName("iconImg");
				
				searchTxt.setTitle("Zoek");
				searchImg.setTitle("Zoek");
			}
			if(type == "export") {
				exportImg = new Image("img/export.png");
				exportTxt = new HTML("Exporteer");
				
				iconFlexTable.setWidget(0, 2, exportImg);
				iconFlexTable.setWidget(0, 3, exportTxt);
				
				exportImg.addStyleName("iconImg");
				exportTxt.addStyleName("iconTxt");
				
				exportTxt.setTitle("Exporteer");
				exportImg.setTitle("Exporteer");
			}
			if(type == "next") {
				nextImg = new Image("img/next.png");
				nextTxt = new HTML("Volgende");
				
				iconFlexTable.setWidget(0, index, nextImg);
				iconFlexTable.setWidget(0, index + 1, nextTxt);
				
				nextImg.addStyleName("iconImg");
				nextTxt.addStyleName("iconTxt");
				
				nextTxt.setTitle("Volgende");
				nextImg.setTitle("Volgende");
			}
		}
	}
	
	public static class Window {
		
		private HorizontalPanel titleBar;
		
		public Window(Panel p, String title, int w, int h) {
			String width = w + "px";
			String height = h + "px";
			titleBar = new HorizontalPanel();
			
			window.clear();
			window.addNorth(titleBar, 2);
			window.add(p);	
			window.setHeight(height);
			window.setWidth(width);
			titleBar.setStyleName("titleBar");
			titleBar.setWidth(width);
			titleBar.add(new HTML(title));
			window.setVisible(true);
		}	
	}

	public static void closeWindow() {
		window.clear();
		window.setVisible(false);
	}
	
	public static void clearIcons() {
		int cols = iconFlexTable.getCellCount(0);
		for(int i=5; i<=cols; i++) {
			iconFlexTable.removeCell(0, 4);
		}
		
		new UserInterface.Icon("export", 2);
		ClickHandler handler = new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Export();	
			}	
		};
		exportImg.addClickHandler(handler);
		exportTxt.addClickHandler(handler);
	}
}
